<script setup lang="ts">
import { useAddressStore } from '@/stores/modules/address'
import type { AddressItem } from '@/types/address'
import { onLoad, onShow } from '@dcloudio/uni-app'
import { ref } from 'vue'

// 子掉父方法，实现点击子组件的关闭，关闭父组件的弹出层
const emit = defineEmits<{
  (event: 'close'): void
}>()

// 获取父组件给的地址列表数据
const query = defineProps<{
  addressList: AddressItem[]
  defaultIndex: number
}>()

// 记录点击地址的下标
const changeIndex = ref(query.defaultIndex)

// 使用pinan缓存
const addressStore = useAddressStore()

// 点击收货地址
const changeAddress = (val: AddressItem, index: number) => {
  changeIndex.value = index
  // 点击的地址传入pinia中保存起来，在上一个页面使用
  addressStore.changeSelectedAddress(val)
  /*
  1、保存当前选中的地址到pinia中，
  2、用计算属性展示父页面中地址的值
  3、从pinia中获取当前选中的默认地址的id传递给立即购买页面
  */
}

// 点击确定
// const submitAddress = () => {
//   if (addressStore.selectedAddress) {
//     // 调用父组件关闭关闭
//     emit('close')
//   } else {
//     // 将地址列表第一条数据存入缓存：当前选中的地址中
//     addressStore.changeSelectedAddress(query.addressList[0])
//     emit('close')
//   }
// }
</script>

<template>
  <view class="address-panel">
    <!-- 关闭按钮 -->
    <text class="close icon-close" @tap="emit('close')"></text>
    <!-- 标题 -->
    <view class="title">配送至</view>
    <!-- 内容 -->
    <view class="content">
      <view
        v-for="(item, index) in query.addressList"
        :key="item.id"
        class="item"
        @tap="changeAddress(item, index)"
      >
        <view class="user">{{ item.receiver }} {{ item.contact }}</view>
        <view class="address">{{ item.fullLocation }} {{ item.address }}</view>
        <text class="icon" :class="index === changeIndex ? 'icon-checked' : ''"></text>
      </view>
    </view>
    <view class="footer">
      <navigator
        @tap="emit('close')"
        class="button primary"
        url="/pagesMember/address-form/address-form"
      >
        新建地址
      </navigator>
      <view @tap="submitAddress" v-if="false" class="button primary">确定</view>
    </view>
  </view>
</template>

<style lang="scss">
.address-panel {
  padding: 0 30rpx;
  border-radius: 10rpx 10rpx 0 0;
  position: relative;
  background-color: #fff;
}

.title {
  line-height: 1;
  padding: 40rpx 0;
  text-align: center;
  font-size: 32rpx;
  font-weight: normal;
  border-bottom: 1rpx solid #ddd;
  color: #444;
}

.close {
  position: absolute;
  right: 24rpx;
  top: 24rpx;
}

.content {
  min-height: 300rpx;
  max-height: 540rpx;
  overflow: auto;
  padding: 20rpx;
  .item {
    padding: 30rpx 50rpx 30rpx 60rpx;
    background-size: 40rpx;
    background-repeat: no-repeat;
    background-position: 0 center;
    background-image: url(https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/images/locate.png);
    position: relative;
  }
  .icon {
    color: #999;
    font-size: 40rpx;
    transform: translateY(-50%);
    position: absolute;
    top: 50%;
    right: 0;
  }
  .icon-checked {
    color: #27ba9b;
  }
  .icon-ring {
    color: #444;
  }
  .user {
    font-size: 28rpx;
    color: #444;
    font-weight: 500;
  }
  .address {
    font-size: 26rpx;
    color: #666;
  }
}

.footer {
  display: flex;
  justify-content: space-between;
  padding: 20rpx 0 40rpx;
  font-size: 28rpx;
  color: #444;

  .button {
    flex: 1;
    height: 72rpx;
    text-align: center;
    line-height: 72rpx;
    margin: 0 20rpx;
    color: #fff;
    border-radius: 72rpx;
  }

  .primary {
    color: #fff;
    background-color: #27ba9b;
  }

  .secondary {
    background-color: #ffa868;
  }
}
</style>
